package org.flexspeedup.command
{
import mx.core.FlexGlobals;

import org.swizframework.utils.chain.ChainStepCommand;

/**
 * Resolves the settings file URL path either from flashvars or from an
 * application-wide default value.
 */
public class SetSettingsUrlCommand  extends ChainStepCommand
{

    //--------------------------------------------------------------------------
    //
    //  Constants
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  DEFAULT_URL
    //----------------------------------
    /**
     * @private
     * The value for the default URL settings file path. If the 
     * application-wide <code>defaultUrl</code> property is not set and/or the 
     * flashvars <code>settingsUrl</code> parameter does not resolve properly, 
     * this constant value will be used.
     */
    private static const DEFAULT_URL:String = "runtime/settings.xml";
    
    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Returns a new instance of this class.
     */
    public function SetSettingsUrlCommand()
    {
    }
    
    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  defaultUrl
    //----------------------------------
    /**
     *  @private
     */    
    private var _defaultUrl:String = DEFAULT_URL;
    
    /**
     * A failback method to set an application-wide default path for the 
     * configuration file.
     * @default "runtime/config.xml"
     */
    public function get defaultUrl():String
    {
        return _defaultUrl;
    }
    
    public function set defaultUrl(value:String):void
    {
        _defaultUrl = value;
    }

    
    //----------------------------------
    // settingsUrl
    //----------------------------------
    [Bindable]
    /**
     * The resolved url for the settings file.
     */
    public var settingsUrl:String;
       
    //--------------------------------------------------------------------------
    //
    //  Overridden Methods - ChainStepCommand
    //
    //--------------------------------------------------------------------------
    /**
     * Attempts to set the config url from flash vars
     */
    override public function execute():void
    {
        settingsUrl = FlexGlobals.topLevelApplication.parameters.settingsUrl;
        
        if (!settingsUrl)
            settingsUrl = defaultUrl;
        complete();
    }
}
}